﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebShopAPI.Models; //前端处理类所有命名空间
using Models;//数据类模型所在命名空间
using DAL;//数据访问类所有命名空间
namespace WebShopAPI.Controllers
{
    /// <summary>
    /// 权限，菜单相关的api方法
    /// </summary>
    public class menusController : ApiController
    {
        sp_permissionDAL permissionDAL = new sp_permissionDAL();
        #region 1 提获取一二级所有菜单
        [HttpGet]
        public ResponData Get()
        {
            List<sp_permission> allPer = permissionDAL.GetAll();
            //读取一级所有数据
            List<sp_permission> firstPer = allPer.Where(p => p.ps_pid == 0).ToList(); //一级的父级为0
            //创建要返回的数据集合，并进行处理
            List<MyMenuItem> allmenu = new List<MyMenuItem>();
            //通过一级循环
            foreach (sp_permission obj in firstPer)
            {
                MyMenuItem menu = new MyMenuItem()
                {
                    id = obj.ps_id,
                    authName = obj.ps_name,
                    // path = "/" + obj.ps_c + "/" + obj.ps_a //二级才有
                };
                //读取该一级数据的二级
                var secondePer = allPer.Where(p => p.ps_pid == obj.ps_id).Select(
                    p => new MyMenuItem
                    {
                        id = p.ps_id,
                        authName = p.ps_name,
                        path = p.ps_c //二级才有
                    }
                    );
                //修改一级的二级菜单集合
                menu.children = secondePer.ToList();
                ///////////////////////////////////////
                allmenu.Add(menu);//加入到所有菜单集合中  

            }
            //////最后构建要响应数据的格式/////////////////////////
            ResponData responData = new ResponData();
            responData.data = allmenu;
            responData.meta.msg = "获取菜单成功";
            responData.meta.status = 200;

            return responData;
        }
        #endregion
    }

    //为提供前端方面显示的数据，需要对数据处理成这样，所以自定义实体类，类写在model中
    /*
        {
    "data":
       [ {
            "id": 101,
            "authName": "商品管理",
            "path": null,
            "children": [
                {
                    "id": 104,
                    "authName": "商品列表",
                    "path": null,
                    "children": []
                }
            ]
        }]
    "meta": {
        "msg": "获取菜单列表成功",
        "status": 200
    }
}
     */
}
